package dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Knowledgepoint entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see dao.Knowledgepoint
 * @author MyEclipse Persistence Tools
 */

public class KnowledgepointDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory
	    .getLogger(KnowledgepointDAO.class);
    // property constants
    public static final String CHAPTERID = "chapterid";
    public static final String DESCRIPTION = "description";
    public static final String DETAILS = "details";
    public static final String KNOWLEDGETYPE = "knowledgetype";
    public static final String EXAMPLE = "example";

    public boolean save(Knowledgepoint transientInstance) {
	return save(log, "saving Knowledgepoint instance", transientInstance);
    }

    public boolean delete(Knowledgepoint persistentInstance) {
	return delete(log, "deleting Knowledgepoint instance", persistentInstance);
    }

    public Knowledgepoint findById(java.lang.Integer id) {
	log.debug("getting Knowledgepoint instance with id: " + id);
	try {
	    Knowledgepoint instance = (Knowledgepoint) getSession().get(
		    "dao.Knowledgepoint", id);
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List<Knowledgepoint> findByExample(Knowledgepoint instance) {
	log.debug("finding Knowledgepoint instance by example");
	try {
	    List<Knowledgepoint> results = (List<Knowledgepoint>) getSession()
		    .createCriteria("dao.Knowledgepoint").add(create(instance))
		    .list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }

    public List findByProperty(String propertyName, Object value) {
	log.debug("finding Knowledgepoint instance with property: "
		+ propertyName + ", value: " + value);
	try {
	    String queryString = "from Knowledgepoint as model where model."
		    + propertyName + "= ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	}
    }

    public List<Knowledgepoint> findByChapterid(Object chapterid) {
	return findByProperty(CHAPTERID, chapterid);
    }

    public List<Knowledgepoint> findByDescription(Object description) {
	return findByProperty(DESCRIPTION, description);
    }

    public List<Knowledgepoint> findByDetails(Object details) {
	return findByProperty(DETAILS, details);
    }

    public List<Knowledgepoint> findByKnowledgetype(Object knowledgetype) {
	return findByProperty(KNOWLEDGETYPE, knowledgetype);
    }

    public List<Knowledgepoint> findByExample(Object example) {
	return findByProperty(EXAMPLE, example);
    }

    public List findAll() {
	log.debug("finding all Knowledgepoint instances");
	try {
	    String queryString = "from Knowledgepoint";
	    Query queryObject = getSession().createQuery(queryString);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }
    public List<Knowledgepoint> findChapterKnowledgePoints(String chapterid) {
	log.debug("finding chapter Knowledgepoint instances");
	try {
	    String queryString = "from Knowledgepoint where chapterid=?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setString(0, chapterid);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }
    
    public List<Knowledgepoint> findPrelevanceKnowledges(int knowledgeid) {
	try {
	    String queryString = "from Knowledgepoint where knowledgeid in(select knowledgeid from Prelevance where prelevanceid=(select prelevanceid from Prelevance where knowledgeid=? and type=0) and type=1)";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, knowledgeid);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }
    public List<List> findSourceKnowledges(String chapterid) {
	try {
	    String queryString = "select new list(knowledgeid,title)from Knowledgepoint where chapterid=?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setString(0, chapterid);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }
    
    public List<List> findDesKnowledges(String chapterid,int knowledgeid) {
   	try {
   	    String queryString = "select new list(knowledgeid,title) from  Knowledgepoint where chapterid=? and knowledgeid !=? and knowledgeid not in(select knowledgeid from Prelevance where prelevanceid=(select prelevanceid from Prelevance where knowledgeid=? and type=0) and type=1)";
   	    Query queryObject = getSession().createQuery(queryString);
   	    queryObject.setString(0, chapterid);
   	    queryObject.setInteger(1, knowledgeid);
   	    queryObject.setInteger(2, knowledgeid);
   	    return queryObject.list();
   	} catch (RuntimeException re) {
   	    throw re;
   	}
       }

    public Knowledgepoint merge(Knowledgepoint detachedInstance) {
	log.debug("merging Knowledgepoint instance");
	try {
	    Knowledgepoint result = (Knowledgepoint) getSession().merge(
		    detachedInstance);
	    log.debug("merge successful");
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public void attachDirty(Knowledgepoint instance) {
	attachDirty(log, "attaching dirty Knowledgepoint instance", instance);
    }
    
    public boolean update(Knowledgepoint instance) {
	return update(log, "updating Knowledgepoint instance", instance);
    }

    public void attachClean(Knowledgepoint instance) {
	log.debug("attaching clean Knowledgepoint instance");
	try {
	    getSession().lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }
    public static void main(String[] args){
	KnowledgepointDAO knowledgepointDAO = new KnowledgepointDAO();
	List<List> lists = knowledgepointDAO.findDesKnowledges("010101",2);
	System.out.println(lists.size());
//	for(List list:lists){
//	    System.out.println(list.get(0)+"--"+list.get(1));
//	}
    }
}